package com.minggo.charmword.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.os.Parcelable;

import com.minggo.charmword.R;
import com.minggo.charmword.logic.CopyDatabaseUtil;
import com.minggo.charmword.service.CharmWordApplication;
import com.minggo.charmword.service.CharmWordService;
import com.minggo.charmword.util.PlanUtil;
import com.minggo.charmword.view.WordDialog;

/**
 * 欢迎界面
 * 
 * @author minggo
 * @date 2014-10-22 下午5:21:07
 */
public class WelcomeActivity extends Activity {
	private boolean isFirst;// 判断是不是第一次打开运用
	private SharedPreferences preferences;
	private WordDialog wordDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_welcome);
		this.startService(new Intent(this, CharmWordService.class));
		preferences = getSharedPreferences("first", Context.MODE_PRIVATE);
		isFirst = preferences.getBoolean("isfrist", true);
		makeShortCut(isFirst);
		
		if (isFirst) {
			wordDialog = new WordDialog(this, R.style.select_dialog, WordDialog.LOADING, getString(R.string.copy_database_title), getString(R.string.copy_database_content));
			wordDialog.show();
			new CopyDatabaseUtil(WelcomeActivity.this, handler).execute();
		} else {
			new Handler().postDelayed(new Runnable() {

				@Override
				public void run() {

					Intent intent = new Intent(WelcomeActivity.this, IndexActivity.class);
					intent.putExtra("from", "welcome");
					startActivity(intent);
					finish();
				}
			}, 1500);
		}
		CharmWordApplication.allActivities.add(this);
	}

	private Handler handler = new Handler(new Callback() {

		@Override
		public boolean handleMessage(Message msg) {
			switch (msg.what) {
			case CopyDatabaseUtil.COPY_DATABASE:
				wordDialog.dismiss();
				wordDialog.cancel();
				wordDialog = null;
				Intent intent = new Intent(WelcomeActivity.this, PlanActivity.class);
				intent.putExtra("from", "welcome");
				startActivity(intent);
				finish();
				break;
			default:
				break;
			}
			return false;
		}
	});

	/**
	 * 设置快捷键
	 * 
	 * @param isFirst
	 */
	private void makeShortCut(boolean isFirst) {

		if (isFirst) {
			Intent shortcutIntent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
			shortcutIntent.putExtra("duplicate", false);
			shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, getString(R.string.app_name));
			Parcelable icon = Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.logo);
			shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, icon);
			Intent intent = new Intent(getApplicationContext(), IndexActivity.class);
			intent.setAction("android.intent.action.MAIN");
			intent.addCategory("android.intent.category.LAUNCHER");
			shortcutIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, intent);
			sendBroadcast(shortcutIntent);
			// 设置不是第一次进入标尺
			SharedPreferences.Editor editor = preferences.edit();
			editor.putBoolean("isfrist", false);
			editor.commit();
		}
	}
}
